﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;

using KC.Framework.Tenant;
using KC.Web.Account.Models;
using Microsoft.AspNetCore.Authorization;
using KC.Framework.Base;

namespace KC.Web.Account.Controllers
{
    public class HomeController : MVC.Core.Controllers.TenantWebBaseController
    {
        public HomeController(
            Tenant tenant,
            IServiceProvider serviceProvider,
            ILogger<HomeController> logger)
            : base(tenant, serviceProvider, logger)
        {
        }

        //[Microsoft.AspNetCore.Authorization.Authorize]
        [MVC.Core.Extension.PermissionFilter("系统管理首页", "首页页面", "Home/Index", ApplicationConstant.DefaultAuthorityId,
            DefaultRoleId = RoleConstants.DefaultRoleId, Order = 1, IsPage = true, ResultType = ResultType.ActionResult, IsDeleted = true)]
        public IActionResult Index()
        {
            ViewBag.UserName = base.CurrentUserName;
            ViewBag.UserEmail = base.CurrentUserEmail;
            ViewBag.UserPhone = base.CurrentUserPhone;
            ViewBag.UserDisplayName = base.CurrentUserDisplayName;
            ViewBag.TenantDisplayName = base.CurrentUserTenantDisplayName;
            return View();
        }

        #region 菜单配置
        [MVC.Core.Extension.MenuFilter("系统管理", "系统管理", "/Home/Enterprise",
            Version = TenantConstant.DefaultVersion, TenantType = TenantConstant.DefaultTenantType, SmallIcon = "fa fa-cogs", AuthorityId = "8D4AB971-7EB6-42DC-B40E-6CF7590381F1",
            DefaultRoleId = RoleConstants.AdminRoleId, Order = 2, IsExtPage = false, Level = 1)]
        public IActionResult Enterprise()
        {
            return base.ThrowErrorJsonMessage(true, string.Empty);
        }

        [MVC.Core.Extension.MenuFilter("系统管理", "组织管理", "/Home/Organization",
            Version = TenantConstant.DefaultVersion, TenantType = TenantConstant.DefaultTenantType, SmallIcon = "fa fa-sitemap", AuthorityId = "6A773F5C-8BE0-4381-8036-1B6F948015CB",
            DefaultRoleId = RoleConstants.AdminRoleId, Order = 2, IsExtPage = false, Level = 2)]
        public IActionResult Organization()
        {
            return base.ThrowErrorJsonMessage(true, string.Empty);
        }

        [MVC.Core.Extension.MenuFilter("系统管理", "用户管理", "/Home/User",
            Version = TenantConstant.DefaultVersion, TenantType = TenantConstant.DefaultTenantType, SmallIcon = "fa fa-user", AuthorityId = "495C2EB0-8772-4430-9952-5B528F41605F",
            DefaultRoleId = RoleConstants.AdminRoleId, Order = 3, IsExtPage = false, Level = 2)]
        public IActionResult User()
        {
            return base.ThrowErrorJsonMessage(true, string.Empty);
        }

        [MVC.Core.Extension.MenuFilter("系统管理", "菜单权限管理", "/Home/MenuPermission",
            Version = TenantConstant.DefaultVersion, TenantType = TenantConstant.DefaultTenantType, SmallIcon = "fa fa-bars", AuthorityId = "986CA9CE-7AB2-411D-83C5-CFB3C65D2FFB",
            DefaultRoleId = RoleConstants.AdminRoleId, Order = 4, IsExtPage = false, Level = 2)]
        public IActionResult MenuPermission()
        {
            return base.ThrowErrorJsonMessage(true, string.Empty);
        }
        #endregion

        #region 辅助方法
        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        [ResponseCache(Duration = 3600, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult AccessDenied(string returnUrl = null)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        [HttpGet]
        [AllowAnonymous]
        public ActionResult DownLoadLogFile(string name)
        {
            var url = ServerPath + "/logs/" + name + ".log";
            return File(url, "text/plain");
        }
        #endregion
    }
}
